package com.ideafactory.client.ybservice.xmpphandler;

import android.util.Log;

import com.ideafactory.client.utils.HandleMessageUtils;
import com.ideafactory.client.utils.HeartBeatClient;
import com.ideafactory.client.utils.HintViewUtils;
import com.ideafactory.client.utils.SoundControl;
import com.ideafactory.client.utils.TYTool;
import com.ideafactory.client.utils.ThreadUitls;
import com.ideafactory.client.utils.caches.CacheUtils;
import com.ideafactory.client.utils.power.PowerOffTool;
import com.ideafactory.client.utils.versioncontrol.UpdateVersionControl;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by LiuShao on 2016/5/11.
 */
public class CoreInfoHandler {

    public static final int ONLINE_TYPE = 1;// 上线
    public static final int VOICE_TYPE = 3;// 声音
    public static final int CUTSCREN_TYPE = 4;// 截屏
    public static final int RUNSET_TYPE = 5;// 设备开关机设置
    public static final int SHOW_SERNUM = 6;// 显示设备编号
    public static final int SHOW_VERSION = 7;// 显示版本号
    public static final int SHOW_DISK_IFNO = 8;// 获取磁盘容量
    public static final int POWER_RELOAD = 9;// 设备重启
    public static final int PUSH_TO_UPDATE = 10;//推送更新
    public final static int HARDWARE_UPDATE = 11;//通知设备硬件更新
    public final static int HARDWARESCREENROTATE_UPDATE = 12;//通知设备硬件屏幕角度更新
    public final static int SET_SYSTME_TIME = 13;//设置系统时间

    public static void messageReceived(String message) {
        Log.e("接收消息", message);
        try {
            JSONObject mesJson = new JSONObject(message);
            Integer type = mesJson.getInt("type");
            JSONObject contentJson;
            switch (type.intValue()) {
                case ONLINE_TYPE:
                    // 系统登录
                    contentJson = mesJson.getJSONObject("content");
                    if (!contentJson.isNull("serNum")) {
                        //第一次系统启动的时候服务器没有设备详细信息，需要向设备传消息
                        MachineDetial.getInstance().upLoadHardWareMessage();
                        String serNum = (String) TYTool.getJsonObj(contentJson, "serNum", "");
                        String pwd = (String) TYTool.getJsonObj(contentJson, "pwd", "");
                        CacheUtils.putSerNumber(serNum);
                        CacheUtils.putPwd(pwd);
                    }

                    Integer dtype = (Integer) TYTool.getJsonObj(contentJson, "dtype", -1);
                    CacheUtils.putDTypeKey(dtype + "");

                    break;
                case VOICE_TYPE:
                    // 声音控制
                    JSONObject jsonObject = mesJson.getJSONObject("content");
                    if (jsonObject != null) {
                        SoundControl.getInstance().setMusicSound(jsonObject.getDouble("voice"));
                    }
                    break;
                case CUTSCREN_TYPE:
                    // 截图控制

//                    HandleMessageUtils.getInstance().runInThread(new Runnable() {
//                        @Override
//                        public void run() {
                            ScreenShot.getInstanse().shootScreen();
//                        }
//                    });
                    break;
                case RUNSET_TYPE:
                    // 开关机时间设置
                    ThreadUitls.runInThread(new Runnable() {
                        @Override
                        public void run() {// 开关机时间设置
                            PowerOffTool.getPowerOffTool().getPowerOffTime(HeartBeatClient.getDeviceNo());
                        }
                    });
                    break;
                case SHOW_SERNUM:
                    // 显示设备编号
                    HintViewUtils.showMyToast(CacheUtils.getSerNumber(), -1);
                    break;
                case SHOW_VERSION:
                    // 版本信息
                    ResourceUpdate.uploadAppVersion();
                    break;
                case SHOW_DISK_IFNO:
                    contentJson = mesJson.getJSONObject("content");
                    Integer flag = (Integer) TYTool.getJsonObj(contentJson, "flag", null);
                    if (flag != null) {
                        if (flag.intValue() == 0) {
                            // 显示
                            ResourceUpdate.uploadDiskInfo();
                        } else if (flag.intValue() == 1) {
                            // 清理磁盘
                            ResourceUpdate.deleteOtherFile();
                            ResourceUpdate.uploadDiskInfo();
                        }
                    }
                    break;
                case POWER_RELOAD:
                    // 机器重启
                    contentJson = mesJson.getJSONObject("content");
                    Integer restart = (Integer) TYTool.getJsonObj(contentJson, "restart", null);
                    if (restart != null) {
                        if (restart.intValue() == 0) {
                            // 关机
                            PowerOffTool.powerShutdown();
                        } else if (restart.intValue() == 1) {
                            // 重启
                            PowerOffTool.restart();
                        }
                    }
                    break;
                case PUSH_TO_UPDATE:
                    UpdateVersionControl.getInstance().checkUpdate();
                    break;
                case HARDWARE_UPDATE:
                    MachineDetial.getInstance().upLoadHardWareMessage();
                    break;
                case HARDWARESCREENROTATE_UPDATE:
                    contentJson = mesJson.getJSONObject("content");
                    Integer rotate = (Integer) TYTool.getJsonObj(contentJson, "screenRotate", null);
                    CacheUtils.putRotate(String.valueOf(rotate));//保存要设置的旋转角度
                    RotateScreen.getInstance().rotateScreen(String.valueOf(rotate));
                    break;
                case SET_SYSTME_TIME:
                    break;
                default:
                    break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
